/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun;

import com.xerox.amazonws.ec2.InstanceType;
import java.util.*;
import java.io.*;

/**
 *
 * @author Barnet Wagman
 */
public class RunJRunExample {

    public void runInParallel() throws NotSerializableException {

            // Create a client object instance.
        Client client = new Client("<Your AWS access key ID>", "Your AWS secret key", "bucketname");
                // The bucketname must identify a bucket that you own.

            // Upload your jars to S3
        client.uploadJars("bucketName",new File("/path/to/dir/with_your/jars"));
                // The jar dir must include all jars that the task class
                // (SomeParallelTask) needs, other than the jars in the JDK.

            // Create you tasks
        List<Runnable> tasks = new ArrayList<Runnable>();
        tasks.add(new SomeParallelTask(1.0));
        tasks.add(new SomeParallelTask(2.0));
        // ...

            // Create EC2 instances
        List<String> iIDs = client.requestInstances(InstanceType.XLARGE_HCPU,"ami-xxx",10);
                // With these params, you'll get 10 "High-CPU Extra Large"
                // (c1.xlarge) instances. Please be sure that you understand
                // the COSTS entailed in renting S3 instances.

            // Submit your tasks for execution.
        List<TaskDescription> tds = client.submit(tasks);
                // This method returns as soon as the tasks are submitted.

            // Get the 'cooked' tasks, i.e. Runnables whose run()
            // methods have been invoked.
        List<Runnable> cookedTasks = client.getCooked(tds);
                // This method blocks until all tasks have been run.

            // Terminate you EC2 instances.
        client.terminateInstances(iIDs);
                // WARNING: if you do not explicitly terminate your
                // EC2 instances they keep running and you'll continue
                // accrue AWS charges.  FYI you do not need to use
                // Client to terminate the instance.  You can do so
                // interactively, using tools like the AWS Management Console
                // or ElasticFox.

            // At this point, information about your tasks and (now defunct)
            // Ec2 instances are stored in SDB.  This methood will
            // clean everything up.
        client.cleanup();
    }

    class SomeParallelTask implements Runnable, Serializable {        
        public SomeParallelTask(double param) { /* ... */ }
        public void run() {
            // ...
        }
    }

    void x() {
        Client client = new Client("<Your AWS access key ID>", "Your AWS secret key", "bucketname");
        List<Runnable> tasks = new ArrayList<Runnable>();
        List<Runnable> trs = ThreadedRunnable.create(tasks,
                                10, // nTasks,
                                7 // nThreads
                                );
        ((ThreadedRunnable) trs.get(0)).getRunnables();
        client.uploadJars(null, null);
    }
}
